package com.cookor.poi.modules.mx.util;

import com.alibaba.fastjson.JSONObject;
import com.cookor.poi.common.utils.IdGen;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class MxUtil {
    public static String testUrl = "https://test-wechat.meditrusthealth.com/gateway/api-equity-sp/spOrder/orderStatusNotify";

    public static String prodUrl = "https://wechat.meditrusthealth.com/gateway/api-equity-sp/spOrder/orderStatusNotify";

    public static String testToken = "f2cd2x357e924c96f8cecf281c2fc49c";

    public static String prodToken = "709684216ea44f9da4c9c775d7g16f7x";

    public static String testId = "91350100MA2Y9NYQ2C";

    public static String prodId = "91350100MA2Y9NYQ2C";



    @Test
    public void test() throws Exception {
        sendNoticestatus(testUrl, testToken, testId, "222", "222", "1", String.valueOf(System.currentTimeMillis()));
    }

    public static void sendNoticestatus(String url, String token, String companyId, String mxOrderNo, String orderNo, String equityCode, String statusTime) throws Exception {
        long timeMillis = System.currentTimeMillis();
        String timestamp = String.valueOf(timeMillis);
        String nonce = IdGen.getRandomStringByLength(8);
        String signature = sign(timestamp, nonce, token);
        String params = "?timestamp=" + timestamp + "&nonce=" + nonce + "&signature=" + signature + "&companyId=" + companyId;
        JSONObject jo = new JSONObject();
        jo.put("mxOrderNo", mxOrderNo);
        jo.put("orderNo", orderNo);
        jo.put("equityCode", equityCode);
        jo.put("quantity", Integer.valueOf(1));
        jo.put("quantityLeft", Integer.valueOf(0));
        jo.put("status", Integer.valueOf(1));
        jo.put("hasReport", Boolean.valueOf(false));
        jo.put("statusTime", statusTime);
        JSONObject param = new JSONObject();
        param.put("requestType", "meditrust.equityinsurance.noticestatus");
        param.put("requestId", IdGen.uuid());
        param.put("requestTime", Long.valueOf(System.currentTimeMillis() / 1000L));
        param.put("companyId", companyId);
        param.put("requestData", jo);
        CloseableHttpClient httpClient = HttpClients.custom().build();
        HttpPost httpPost = new HttpPost(url + params);
        httpPost.setHeader("User-Agent", "poi/my-private-project");
        params = JSONObject.toJSONString(param);
        StringEntity entity = new StringEntity(params);
        entity.setContentEncoding("UTF-8");
        httpPost.setEntity((HttpEntity) entity);
        httpPost.setHeader("Content-Type", "application/json");
        CloseableHttpResponse response = httpClient.execute((HttpUriRequest) httpPost);
        try {
            String json = EntityUtils.toString(response.getEntity());
            System.out.println(json);
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (!jsonObject.getString("resultCode").equals("000000"))
                throw new Exception(jsonObject.getString("resultMsg"));
        } catch (Exception ex) {
            throw ex;
        }
    }

    public static String sign(String timestamp, String nonce, String token) {
        String str = timestamp + nonce + token;
        return Hashing.sha1().hashString(str, Charsets.UTF_8)
                .toString().toUpperCase();
    }
}
